package com.zhu.controller;

import com.zhu.pojo.Result;
import com.zhu.pojo.Stock;
import com.zhu.service.StockService;
import jakarta.persistence.EntityResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/stock")
public class StockController {
    @Autowired
    private StockService stockService;
    @PostMapping("/add")
    public Result addstock(@RequestBody  Stock s){
        boolean b = stockService.addstock(s);
        if(b) return Result.ok();
        return Result.fail("失败");
    }

    @PostMapping("search")
    public Result search(@RequestBody String model){
        List<Stock> list = stockService.search(model);
        if(list.size()==0) return Result.fail("");
        return Result.ok(list);
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody Stock stock){
        try{
            stockService.edit(stock);
            return Result.ok();
        }catch (Exception e){
            return Result.fail("");
        }
    }

    @PostMapping("/searchOne")
    public Result searchOne(@RequestBody String model){
        Stock s = stockService.searchOne(model);
        if(s==null){
            return Result.fail("");
        }
        return Result.ok(s);
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody String model){
        try {
            stockService.delete(model);
            return Result.ok();
        }catch (Exception e){
            return Result.fail("");
        }
    }

    @PostMapping("/areas")
    public List<String> areas(){
        return stockService.areas();
    }
}
